import validHtml from 'ngtemplate!html!../../../tpls/dialog/valid.dialog.html';

class GroupaliasEditController {
    constructor($cookieStore, $state, dialogService, GroupaliasService, getGroupaliasDetail){

        const vm = this;

        vm.$cookieStore        = $cookieStore;
        vm.$state              = $state;
        vm.dialogService       = dialogService;
        vm.GroupaliasService   = GroupaliasService;
        vm.getGroupaliasDetail = getGroupaliasDetail;

        // 表单数据初始化
        vm.groupalias = {
            alias: ''
        }

        console.log(vm.getGroupaliasDetail)

        // 获取分组详情
        vm.getGroupaliasDetail && vm.getGroupaliasDetail.code == 0 && (vm.groupalias.alias = vm.getGroupaliasDetail.data.alias)

    }

    // 表单提交
    submitForm(isValid) {

        const vm = this;

        // 验证失败, 提示用户
        !isValid && !vm.groupalias.alias && $.blink(angular.element(document.getElementsByName('alias')), 5, 100)

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            let params = {
                alias: vm.groupalias.alias
            }

            console.log(vm.$state.params.aid)

            // 更新分组
            vm.GroupaliasService.postGroupaliasUpdate(params, vm.$cookieStore.get('access_token'), vm.$state.params.aid)
            .then( data => {

                // 成功调用
                data.code==0&&vm.dialogService.success('更新成功', 'dashboard.race.groupaliaslist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                
                console.log(data)
            }, error => console.log('error'));

        }

        return false;
    }
}

GroupaliasEditController.$inject = ['$cookieStore', '$state', 'dialogService', 'GroupaliasService', 'getGroupaliasDetail']; 

export default GroupaliasEditController;